package com.zb.tools;


import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Map;

public class ApacheHttpKit {

    private HttpClient client;

    public String get(String url)
    {
        HttpGet get=new HttpGet(url);
        try
        {

            HttpEntity entity=client.execute(get).getEntity();
            return EntityUtils.toString(entity);
        }
        catch (IOException e)
        {
            throw new RuntimeException("get fail:"+e.getMessage());
        }
    }

    public String get(String url, Map<String,String> headers)
    {
        HttpGet get=new HttpGet(url);
        if(headers!=null)
            for(Map.Entry<String,String> kv : headers.entrySet())
                get.addHeader(kv.getKey(),kv.getValue());
        try
        {

            HttpEntity entity=client.execute(get).getEntity();
            return EntityUtils.toString(entity);
        }
        catch (IOException e)
        {
            throw new RuntimeException("get fail:"+e.getMessage());
        }
    }

    public String post(String url, String str, Map<String,String> headers)
    {
        HttpPost post=new HttpPost(url);
        if(headers!=null)
            for(Map.Entry<String,String> kv : headers.entrySet())
                post.addHeader(kv.getKey(),kv.getValue());
        if(str!=null) {
            HttpEntity request_entity = EntityBuilder.create().setText(str).build();
            post.setEntity(request_entity);
        }
        try
        {
            HttpEntity entity=client.execute(post).getEntity();
            return EntityUtils.toString(entity);
        }
        catch (IOException e)
        {
            throw new RuntimeException("get fail:"+e.getMessage());
        }
    }

    public String postForm(String url, Map<String,String> params, Map<String,String> headers) {
        HttpPost post = new HttpPost(url);
        if (headers != null)
            for (Map.Entry<String, String> kv : headers.entrySet())
                post.addHeader(kv.getKey(), kv.getValue());
        if (params != null)
        {
            ArrayList<BasicNameValuePair> data = new ArrayList<BasicNameValuePair>();
            for (Map.Entry<String, String> kv : params.entrySet())
                data.add(new BasicNameValuePair(kv.getKey(), kv.getValue()));
            UrlEncodedFormEntity request_entity=new UrlEncodedFormEntity(data, Charset.forName("utf-8"));
            post.setEntity(request_entity);
        }

        try
        {
            HttpEntity entity=client.execute(post).getEntity();
            return EntityUtils.toString(entity);
        }
        catch (IOException e)
        {
            throw new RuntimeException("get fail:"+e.getMessage());
        }
    }

    public String postJson(String url, String json, Map<String,String> headers)
    {
        HttpPost post=new HttpPost(url);
        if(headers!=null)
            for(Map.Entry<String,String> kv : headers.entrySet())
                post.addHeader(kv.getKey(),kv.getValue());
        post.addHeader("Content-Type", "application/json;charset=utf-8");
        if(json!=null) {
            HttpEntity request_entity = EntityBuilder.create()
                    //.setContentType(ContentType.APPLICATION_JSON)
                    //.setContentEncoding("utf-8")
                    .setText(json)
                    .build();
            post.setEntity(request_entity);
        }
        try
        {
            HttpEntity entity=client.execute(post).getEntity();
            return EntityUtils.toString(entity);
        }
        catch (IOException e)
        {
            throw new RuntimeException("get fail:"+e.getMessage());
        }
    }

    private ApacheHttpKit()
    {
        client= HttpClientBuilder.create().build();
    }

    //===================================================

    public static ApacheHttpKit create()
    {
        return new ApacheHttpKit();
    }
}
